package com.mifengs.mall.floor;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.annotation.ItemProviderTag;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.Constant;
import com.mifengs.mall.R;
import com.mifengs.mall.adapter.ClassifyRvAdapter;
import com.mifengs.mall.entity.AdvertBean;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.event.MainEvent;
import com.mifengs.mall.util.ToIntentUtil;
import com.mifengs.mall.widget.HorizontalPageLayoutManager;
import com.mifengs.mall.widget.IndicatorView;
import com.mifengs.mall.widget.PagingScrollHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by lihaoqi on 2018/4/19.
 */


@ItemProviderTag(
        viewType = FloorListAdapter.TYPE_FOURTEEN,
        layout = R.layout.fragment_classify
)
public class FloorFourteenProvider extends BaseItemProvider<FloorBean,BaseViewHolder> {


    @Override
    public void convert(BaseViewHolder helper, FloorBean floorBean, int position) {
        RecyclerView rvClassify  = helper.getView(R.id.rv_classify);
        final IndicatorView indicatorView = helper.getView(R.id.id_view);
        final List<AdvertBean> advertBeanList = floorBean.getAdverts();
        int size = advertBeanList.size();
        int page = size/10;
        if(size%10>0){
            page = page+1;
        }
        HorizontalPageLayoutManager horizontalPageLayoutManager = new HorizontalPageLayoutManager(2,5);
        indicatorView.setTotalSize(page);
        rvClassify.setLayoutManager(horizontalPageLayoutManager);
        PagingScrollHelper mScrollHelper = new PagingScrollHelper();
        mScrollHelper.setUpRecycleView(rvClassify);
        mScrollHelper.setOnPageChangeListener(new PagingScrollHelper.onPageChangeListener() {
            @Override
            public void onPageChange(int index) {
                indicatorView.setCurrentIndex(index);
            }
        });
        ClassifyRvAdapter adapter = new ClassifyRvAdapter();
        rvClassify.setAdapter(adapter);
        adapter.setNewData(advertBeanList);
        if(size!=0){
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    String appActionUrl  = advertBeanList.get(position).getAppActionUrl();
                    if(TextUtils.isEmpty(appActionUrl)){
                        if(advertBeanList.get(position).getActionUrl().indexOf("shareEarn") !=-1){
                            if("".equals(PreferenceUtils.getToken())){
                                EventBus.getDefault().post(new MainEvent(MainEvent.LOGIN));
                            }else{
                                ToIntentUtil.toCommonIntent(advertBeanList.get(position).getActionUrl(),mContext);
                            }
                        }else{
                            ToIntentUtil.toCommonIntent(advertBeanList.get(position).getActionUrl(),mContext);
                        }

                    }else{
                        if(Constant.STORE_APPLY.equals(appActionUrl)){
                            EventBus.getDefault().post(new MainEvent(MainEvent.OPENSHOP));
                        }
                    }
                }
            });
        }



    }

    @Override
    public void onClick(BaseViewHolder helper, FloorBean floorBean, int position) {

    }

    @Override
    public boolean onLongClick(BaseViewHolder helper, FloorBean floorBean, int position) {
        return false;
    }


}
